package cn.com.generaldata.os.service.lan;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.pojo.Area;
import cn.com.generaldata.os.pojo.Unit;
import cn.com.generaldata.os.pojo.User;
import cn.com.generaldata.os.service.common.AbstractPmService;

@Component
public class PmServiceForLan extends AbstractPmService {

	/**
	 * 根据企业名称检索企业.
	 */
	public List<Unit> queryCompanyByName(String name, String uid) {
		// Filter filter = new Filter();
		String hql = "from Unit u where 1=1";
		HashMap map = new HashMap();
		if (name != null && !"".equals(name)) {
			hql += " and unitName like :name";
			map.put("name", "%" + name + "%");
			// filter.addRule(new FilterRule(FilterConstant.LIKE, "unitName",
			// "%" + name + "%"));
		}
		// 如果是企业用户，只能给海洋局发
		if (uid != null && !"".equals(uid)) {
			hql += " and role.id='4' and  (exists (select a.id from Area a where a.id=u.area.id and a.areaCode is not null and a.parentId=:areaId) or u.area.id=:parentId)";
			User user = userDAO.get(User.class, uid);
			Area area = user.getArea();
			// Filter filter2 = new Filter();
			// filter2.setOrder(FilterConstant.ASC);
			// filter2.setSortBy("areaName");
			map.put("areaId", area.getId());
			map.put("parentId", area.getParentId());
			// filter2.addRule(new FilterRule(FilterConstant.NOT_NULL,
			// "areaCode", null));
			// filter2.addRule(new FilterRule(FilterConstant.EQUAL, "parentId",
			// area.getId()));
			// // 下属
			// List<Area> areas = areaDAO.find(Area.class, filter2, null);
			// List<String> areaIds = new ArrayList<String>();
			// for (Area area1 : areas) {
			// areaIds.add(area1.getId());
			// }
			// // 上属
			// areaIds.add(area.getParentId());
			// // if("3".equals(roleId))
			// // {
			// String[] ids = new String[areaIds.size()];
			// areaIds.toArray(ids);
			// filter.addRule(new FilterRule(FilterConstant.EQUAL, "role.id",
			// "4"));
			// filter.addRule(new FilterRule(FilterConstant.COLLECTION,
			// "area.id", ids));
			// }
		}
		// List<Unit> resultList =
		cn.com.generaldata.os.paging.Result result = unitDAO.hqlParam(hql, map, null);
		List<Unit> resultList = (List<Unit>) result.getOperationResult();
		return resultList;
	}

	/**
	 * 按企业名称查询的结果数.
	 */
	public int countCompanyByName(String name, String role) {
		// Filter filter = new Filter();
		String hql = "from Unit u where 1=1";
		HashMap map = new HashMap();
		if (name != null && !"".equals(name)) {
			// filter.addRule(new FilterRule(FilterConstant.LIKE, "unitName",
			// "%" + name + "%"));
			hql += " and unitName like :name";
			map.put("name", "%" + name + "%");
		}
		// 只能给自己的直属上级单位和直属下级单位发
		if (role != null && !"".equals(role)) {
			hql += " and role.id='4' and  (exists (select a.id from Area a where a.id=u.area.id and a.areaCode is not null and a.parentId=:areaId) or u.area.id=:parentId)";

			User user = userDAO.get(User.class, role);
			Area area = user.getArea();
			map.put("areaId", area.getId());
			map.put("parentId", area.getParentId());
			Filter filter2 = new Filter();
			// filter2.setOrder(FilterConstant.ASC);
			// filter2.setSortBy("areaName");
			// filter2.addRule(new FilterRule(FilterConstant.NOT_NULL,
			// "areaCode", null));
			// filter2.addRule(new FilterRule(FilterConstant.EQUAL, "parentId",
			// area.getId()));
			// // 下属
			// List<Area> areas = areaDAO.find(Area.class, filter2, null);
			// List<String> areaIds = new ArrayList<String>();
			// for (Area area1 : areas) {
			// areaIds.add(area1.getId());
			// }
			// // 上属
			// areaIds.add(area.getParentId());
			// // if("3".equals(roleId))
			// // {
			// String[] ids = new String[areaIds.size()];
			// areaIds.toArray(ids);
			// filter.addRule(new FilterRule(FilterConstant.EQUAL, "role.id",
			// "4"));
			// filter.addRule(new FilterRule(FilterConstant.COLLECTION,
			// "area.id", ids));
			// }
		}
		cn.com.generaldata.os.paging.Result result = unitDAO.hqlParam(hql, map, null);
		// List<Unit> resultList =(List<Unit>) result.getOperationResult();
		// unitDAO.count(Unit.class, filter);
		return result.getTotalCount();
	}

}
